// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let {OPENID}=cloud.getWXContext()
  let db=cloud.database()
  const _ = db.command
  let prizenum=Math.floor(Math.random()*3)+1
  let date=new Date();
  prize= await db.collection("prize")
  .where({number:prizenum})
  .get()

  record= await db.collection("client")
  .where({openid:OPENID})
  .update({
    data:{
      lotteryTimes:_.inc(-1),
      lotteryRecord:_.push({time:date.getFullYear()+'年'+(date.getMonth()+1)+'月'+date.getDate()+'日',prize:prize.data[0].prize})
    }
  })

  

  return {prize,record}
}